﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Student_1 : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userName"] == null || Session["userType"] == null || Session["userType"].ToString() != "SinhVien")
        {
            Response.Redirect("~/Default.aspx");
        }

        string MSSV = Session["userName"].ToString();
        string MSGV = Request.QueryString["MSGV"].ToString();
        string MaMH = Request.QueryString["MaMH"].ToString();
        string HocKy = Request.QueryString["HocKy"].ToString();

        SqlConnection conn = new SqlConnection(connectionString);
        string strSQL = String.Format("select * from ChiTietDanhGia where MSSV='{0}' and MSGV='{1}' and MaMH='{2}' and HocKy='{3}'",
            MSSV, MSGV, MaMH, HocKy);
        SqlDataAdapter da = new SqlDataAdapter(strSQL, conn);
        DataTable dt = new DataTable();
        da.Fill(dt);

        if (dt.Rows.Count != 0)
        {
            GridView1.Enabled = false;
            Button1.Enabled = false;
            ShowMessage("Bạn Đã Đánh Giá Giảng Viên Trước Đó!", "");
        }
    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        try
        {
            string MaTC = "";
            string MSSV = Session["userName"].ToString();

            string MSGV = Request.QueryString["MSGV"].ToString();
            string MaMH = Request.QueryString["MaMH"].ToString();
            string HocKy = Request.QueryString["HocKy"].ToString();

            bool flag = true;

            for (int j = 0; j < GridView1.Rows.Count; j++)
            {
                RadioButtonList rb = GridView1.Rows[j].FindControl("RadioButtonList1") as RadioButtonList;

                if (rb.SelectedValue == "")
                {
                    flag = false;
                }
            }

            if(!flag)
            {
                ShowMessage("Bạn phải đánh giá tất cả tiêu chí!", "");
            }
            else
            {
                int a = 0;

                for (int j = 0; j < GridView1.Rows.Count; j++)
                {
                    RadioButtonList rb = GridView1.Rows[j].FindControl("RadioButtonList1") as RadioButtonList;

                    a += Convert.ToInt32(rb.SelectedValue);

                    MaTC = GridView1.Rows[j].Cells[0].Text;

                    SqlConnection conn = new SqlConnection(connectionString);

                    conn.Open();
                    SqlCommand cmd = new SqlCommand("insert into ChiTietDanhGia values('" + MSSV + "','" + MSGV + "','" + MaMH + "','" + HocKy + "','" + MaTC + "','" + rb.SelectedValue.ToString() + "')", conn);
                    cmd.ExecuteNonQuery();

                }

                lblTongDiem.Text = "Tổng Điểm Đánh Giá: " + a.ToString();
                ShowMessage("Đánh Giá Thành Công", "");
            }
        }
        catch (SqlException ex)
        {
            ShowMessage(ex.Message, "");
        }
    }

    protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
    {
        Logout();
    }
}